<template>
  <div class="view-container">
    <div class="login-form-content">
      <el-form
        ref="loginForm"
        label-position="left"
        label-width="60px"
        :model="userInfo"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userInfo.password"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input v-model="userInfo.verificationCode">
            <template slot="suffix">
              <img :src="verImageUrl" alt="">
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" @click="login">登录</el-button>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { queryVerificationCode } from '../api/login'
export default {
  name: 'Login',
  data () {
    return {
      userInfo: {
        username: '15237136491',
        password: '123456',
        verificationCode: ''
      },
      verImageUrl: ''
    }
  },
  created () {
    queryVerificationCode().then(res => {
      if (res.data.code === 200) {
        this.verImageUrl = res.data.data
      }
    })
  },
  methods: {
    ...mapActions(['userLogin']),
    login () {
      const { userLogin } = this
      userLogin({
        username: this.userInfo.username,
        password: this.userInfo.password,
        verificationCode: this.userInfo.verificationCode
      }).then(() => {
        this.$router.push('/')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.view-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-flow: row nowrap;
  justify-content: center;
  align-items: center;
  & .login-form-content {
    width: 600px;
    height: 600px;
  }
}
</style>
